<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/6-17:05
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\wechat\service;


    use think\Db;

    class FansService
    {

        /**
         * @param array $user
         * @param string $appid
         * @return bool
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/6
         * @name: set
         * @describe:增加或更新粉丝信息
         */
        public static function set(array $user, $appid = '')
        {
            if (!empty($user['subscribe_time'])) {
                $user['subscribe_at'] = date('Y-m-d H:i:s', $user['subscribe_time']);
            }
            if (isset($user['tagid_list']) && is_array($user['tagid_list'])) {
                $user['tagid_list'] = is_array($user['tagid_list']) ? join(',', $user['tagid_list']) : '';
            }
            if ($appid !== '') {
                $user['appid'] = $appid;
            }
            unset($user['privilege'], $user['groupid']);

            return dataSave('WechatFans', $user, 'openid');
        }

        /**
         * @param $openid
         * @return array|\PDOStatement|string|\think\Model|null
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/6
         * @name: get
         * @describe:获取粉丝信息
         */
        public static function get($openid)
        {
            return Db::name('WechatFans')->where(['openid' => $openid])->find();
        }


    }